package cn.aylog.search;

import java.util.Scanner;

/**
 * 哈希算法
 */
public class hashTable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        HashTab hashTable = new HashTab(8);
        while(true) {
            System.out.println("add: 添加员工");
            System.out.println("list: 显示员工");
            System.out.println("find: 查找员工");
            System.out.println("exit: 退出");
            System.out.println("请选择功能:");
            String key = sc.next();
            switch (key) {
                case "add":
                    System.out.println("请输入id:");
                    int id = sc.nextInt();
                    System.out.println("请输入名称:");
                    String name = sc.next();
                    hashTable.add(new Emp(id, name));
                    break;
                case "list":
                    hashTable.show();
                    break;
                case "find":
                    System.out.println("请输入id:");
                    id = sc.nextInt();
                    Emp emp = hashTable.findById(id);
                    System.out.println(emp);
                    break;
                case "exit":
                    sc.close();
                    System.exit(0);
                    break;
                default:
                    break;
            }
        }
    }
}

/**
 * hash散列表
 */
class HashTab {
    private EmpLink[] list;
    private int size;
    public HashTab(int size) {
        this.size = size;
        list = new EmpLink[size];
        for(int i=0; i<size; i++) {
            list[i] = new EmpLink();
        }
    }

    public void add(Emp emp) {
        int hashIndex = getHash(emp.id);
        list[hashIndex].add(emp);
    }

    public int getHash(int id) {
        return id % size;
    }

    public void show() {
        for(int i=0; i<list.length; i++) {
            list[i].list();
        }
    }

    public Emp findById(int id) {
        int hashIndex = getHash(id);
        EmpLink link = list[hashIndex];
        return link.find(id);
    }
}

/**
 * 员工信息列表
 */
class EmpLink {
    private Emp head;

    public void add(Emp emp) {
        if(head == null) {
            head = emp;
            return;
        }
        Emp temp = head;
        while (temp != null) temp = temp.next;
        temp.next = emp;
    }

    public void list() {
        if(head == null) {
            System.out.println("当前链表为空!");
            return;
        }
        Emp temp = head;
        System.out.print("当前链表数据为:");
        while(temp != null) {
            System.out.print("->id="+temp.id+",name="+temp.name);
            temp = temp.next;
        }
        System.out.println();
    }

    public boolean isEmpty() {
        return head == null;
    }

    public Emp find(int id) {
        if(isEmpty()) {
            System.out.println("数据为空");
            return null;
        }
        Emp temp = head;
        while (temp != null) {
            if(temp.id == id) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }
}


/**
 * 员工信息类
 */
class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
